home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.gu.edu.au!usenet
- From: Student (Student)
- Newsgroups: comp.lang.c
- Subject: Re: help rand !
- Date: 19 Apr 1996 06:27:09 GMT
- Organization: Griffith University
- Message-ID: <4l7bnt$r44@griffin.itc.gu.edu.au>
- References: <4l5d75$mes@usenetp1.news.prodigy.com> <4l5fj4$1ehs@serra.unipi.it> <4l5vjm$5lj@ccshst05.uoguelph.ca>
- NNTP-Posting-Host: enslab29.student.gu.edu.au
- X-Newsreader: WinVN 0.92.5
-
- In article <4l5vjm$5lj@ccshst05.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) says:
- >
- >Maurizio Loreti (loreti@mxsld2.pd.infn.it) wrote:
- >: In article <4l5d75$mes@usenetp1.news.prodigy.com>, UNCK19A@prodigy.com (Thomas Furka) writes:
- >: >I AM WRITING A PROGRAM FOR SCHOOL THAT PICKS 12 RANDOM NUMBERS. COULD
- >: >SOME ONE PLEASE HELP ME SO THAT I DON'T GET ANY DUPLICATES. I AM PICKING
- >: >FROM THE COMPUTER CLOCK(<TIME.H> USING SRAND.
- >
- >: Don't shout; we are not blind.
- whoever said that last bit is cool!
-
- but what you want to know is:
- There is a really excellent algorithm with source
- for generating random numbers with no repeats
- available from Abe's Demo School
- http://www.mds.mdh.se/~dat94avi/demoschool.htm
- It is in his digital effekt.
- The routine is very much faster than simply
- disgarding unwanted results which can get realll sllooow
- near the end of a list if the length is long.
- However the source is beyond the scope of a school assignment.
- I assume you want something that does this:
- 10 random -> 9,3,5,1,7,8,2,4,10,6
- You could consider creating an array n[n]
- where n represents the highest number
- and then just randomly swaping the contents for every n.
- ie: before: n[1] = 1
- n[2] = 2
- : :
- n[10]=10
-
- swap n[1] with n[rand]
- swap n[2] with n[rand]
- ......................
- swap n[10] with n[rand]
-
- after: n[1] = 9
- n[2] = 3
- : :
- n[10]= 6
-
- This ain't that great, but it will work predictably.
-